package com.imooc.service.impl.center;

import com.github.pagehelper.PageHelper;
import com.imooc.enums.OrderStatusEnum;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.OrderStatusMapper;
import com.imooc.mapper.OrdersMapper;
import com.imooc.mapper.OrdersMapperCustom;
import com.imooc.pojo.OrderStatus;
import com.imooc.pojo.Orders;
import com.imooc.pojo.vo.MyOrdersVO;
import com.imooc.pojo.vo.OrderStatusCountsVO;
import com.imooc.service.center.MyOrdersService;
import com.imooc.service.impl.BaseService;
import com.imooc.utils.PagedGridResult;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @ClassName: CenterUserServiceImpl
 * @Package: com.imooc.service.impl
 * @Description:
 * @Date: 2021/7/5 11:26
 * @Author: sunzheng@beiming.com
 */
@Service
public class MyOrdersServiceImpl extends BaseService implements MyOrdersService {

  @Autowired
  private OrdersMapperCustom ordersMapperCustom;

  @Autowired
  private OrderStatusMapper orderStatusMapper;

  @Autowired
  private OrdersMapper ordersMapper;

  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public boolean updateReceiveOrderStatus(String orderId) {
    OrderStatus updateOrder = new OrderStatus();
    updateOrder.setOrderStatus(OrderStatusEnum.SUCCESS.type);
    updateOrder.setSuccessTime(new Date());

    Example example = new Example(OrderStatus.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("orderId", orderId);
    criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
    int result = orderStatusMapper.updateByExampleSelective(updateOrder, example);
    return result == 1;
  }

  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public boolean deleteOrder(String userId, String orderId) {

    Orders deleteOrder = new Orders();
    deleteOrder.setIsDelete(YesOrNo.YES.type);
    deleteOrder.setUpdatedTime(new Date());

    Example example = new Example(Orders.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("id", orderId);
    criteria.andEqualTo("userId", userId);
    int result = ordersMapper.updateByExampleSelective(deleteOrder, example);
    return result == 1;
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public Orders queryMyOrder(String userId, String orderId) {
    Orders order = new Orders();
    order.setUserId(userId);
    order.setId(orderId);
    order.setIsDelete(YesOrNo.NO.type);
    return ordersMapper.selectOne(order);
  }

  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public void updateDeliverOrderStatus(String orderId) {

    OrderStatus updateOrder = new OrderStatus();
    updateOrder.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
    updateOrder.setDeliverTime(new Date());

    Example example = new Example(OrderStatus.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("orderId", orderId);
    criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);

    orderStatusMapper.updateByExampleSelective(updateOrder, example);


  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult getOrderTrend(String userId, Integer page, Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);
    PageHelper.startPage(page, pageSize);

    List<OrderStatus> list = ordersMapperCustom.getMyOrderTrend(map);

    return setPageGrid(list, page);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public OrderStatusCountsVO getOrderStatusCounts(String userId) {
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);
    map.put("orderStatus", OrderStatusEnum.WAIT_PAY.type);
    int waitPayCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
    int waitDeliverCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
    int waitDeceiveCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    map.put("orderStatus", OrderStatusEnum.SUCCESS.type);
    map.put("isComment", YesOrNo.NO.type);
    int waitCommentCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    OrderStatusCountsVO countsVO = new OrderStatusCountsVO(waitPayCounts, waitDeliverCounts,
        waitDeceiveCounts, waitCommentCounts);

    return countsVO;
  }

  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public PagedGridResult queryMyOrders(String userId, Integer orderStatus, Integer page,
      Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);
    if (orderStatus != null) {
      map.put("orderStatus", orderStatus);
    }

    PageHelper.startPage(page, pageSize);

    List<MyOrdersVO> list = ordersMapperCustom.queryMyOrders(map);

    return setPageGrid(list, page);
  }

}
